package com.sh.data.engine.web.webapi.controller.api;

import com.sh.data.engine.application.service.api.DataApiSecurityManageApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.api.model.domain.DataApiSecurityManageDomain;
import com.sh.data.engine.domain.api.service.DataApiSecurityManageService;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author : fuchen
 * @date : 2022/2/10 18:21
 */
@RestController
@Tag(name = "数据服务-安全管理", description = "数据服务-安全管理")
@Slf4j
@RequestMapping("/dataApiSecurityManage")
public class DataApiSecurityManageController {

    @Autowired
    private DataApiSecurityManageService dataApiSecurityManageService;

    @Autowired
    private DataApiSecurityManageApplicationService dataApiSecurityManageApplicationService;

    @Operation(summary = "授权审批列表", description = "授权审批列表")
    @GetMapping("/getApiSecurityManagePage")
//    @AuthorityAnnotation(menuId = MenuEnumNew.AQGL, readWriteStatus = 0)
    public RestResponseEntity<PageResult<DataApiSecurityManageDomain>> getApiSecurityManagePage(
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
        @RequestParam(required = false) String apiName) {
        var result = dataApiSecurityManageApplicationService.getApiSecurityManagePage(apiName, pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "编辑授权列表", description = "编辑授权列表")
    @GetMapping("/getEditApiSecurityManagePage")
//    @AuthorityAnnotation(menuId = MenuEnumNew.AQGL, readWriteStatus = 0)
    public RestResponseEntity<PageResult<DataApiSecurityManageDomain>> getEditApiSecurityManagePage(
        Long apiId, String username, Integer pageNum, Integer pageSize) {
        var result = dataApiSecurityManageApplicationService.getEditApiSecurityManagePage(
            apiId, username, pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "取消授权", description = "取消授权")
    @PostMapping("/cancelApiSecurityManageById")
    @LogExecution(operation = EngineOperation.CACEL_AUTHRIZATION, business = Business.数据服务)
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.CACEL_AUTHRIZATION)
//    @AuthorityAnnotation(menuId = MenuEnumNew.AQGL, readWriteStatus = 1)
    public RestResponseEntity<Boolean> cancelApiSecurityManageById(
        @RequestParam(required = true, name = "userId") String userId,
        @RequestParam(required = true, name = "apiId") Long apiId) {
        var result = dataApiSecurityManageApplicationService.cancelApiSecurityManageById(userId, apiId);
        return RestResponseEntity.success(result);
    }
}
